# 设置根文件夹路径
$rootFolder = "D:\BaiduNetdiskDownload\haitao"
# $rootFolder = "D:\workspace\2024\test\haitaotest"

# 遍历一级子文件夹
Get-ChildItem -Path $rootFolder -Directory | ForEach-Object {
    $subFolder = $_.FullName
    $mrFolder = Join-Path -Path $subFolder -ChildPath "MR"

    # Write-Host $mrFolder

    if (Test-Path $mrFolder) {
        # 确保 MR 文件夹中只包含指定六个文件夹
        $subFolders = Get-ChildItem -Path $mrFolder -Directory | Select-Object -ExpandProperty Name;
        $requiredFolders = @("DWI", "T1_A", "T1_D", "T1_PRE", "T1_V", "T2");
        
        # 两个文件夹组不一样，则会输出 123
        if (Compare-Object $subFolders $requiredFolders) {
            Write-Host "123"
        }

        # 检测 6 个序列文件夹下的内容
        # 1. 每一个 MR folder
        $mrFolder | ForEach-Object {
            $d1 = $_
            # 2. MR 下每一个 序列 folder
            $requiredFolders | ForEach-Object {
                $dstFolder = Join-Path -Path $d1 -ChildPath $($_)

                # Write-Host "Dst Folder: "$dstFolder

                # 3. 目的文件夹下 所有文件
                Get-ChildItem -Path $dstFolder -File -Filter "*.nii.gz" | Where-Object { $_.BaseName.Length -gt 10 } | ForEach-Object {
                    Write-Host "Need delete is: $_"
                    Remove-Item $_.FullName -Force
                }
            }
        }
    }
}

Write-Host "Task completed."
